package com.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.common.uilt.BaseAction;
import com.common.uilt.ConfigUtil;
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.model.Messages;
import com.model.RoleBean;
import com.model.UserBean;
import com.service.imp.RoleService;
import com.service.imp.UserService;
@Controller
@ParentPackage("defaultpackage")
@Namespace("/back")
@Action(value = "system", results = {
		@Result(name="user_list",location="/back/system/userlist.jsp"),
		@Result(name="role_list",location="/back/system/editrole.jsp"),
		@Result(name="role_menu",location="/back/system/editroleMenu.jsp"),
		//@Result(name="browseentry",location="assetsMS!browseentry",type="redirectAction"),
		@Result(name="systemsettings",location="/back/system/systemsettings.jsp"),
		@Result(name = "index", location = "/back/index.jsp"),
		@Result(name = "refsign",type="redirectAction",location="../hello"),
		@Result(name = "error", location = "/hello.jsp"), })
public class SystemAction  extends BaseAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 2298990181740908951L;
	private static final Logger logger = Logger
			.getLogger(SystemAction.class);
	@Autowired
	private UserService userService;
	@Autowired
	private RoleService roleService;

	private String role_id_array;
	
	private String user_id;
	private UserBean user;
	private String menuid;

	private String rolename;
	private RoleBean role;
	private String roleid;
/**
 * 系统设置
 * @return
 */
	public String systemsetting(){
		request.setAttribute("serialhead", ConfigUtil.getFileIo("serial.head",this.configpath));
		request.setAttribute("serialinterval", ConfigUtil.getFileIo("serial.interval",this.configpath));
		request.setAttribute("serialno", ConfigUtil.getFileIo("serial.no",this.configpath));
		request.setAttribute("is_auto_number", ConfigUtil.getFileIo("is_auto_number",this.configpath));
		return "systemsettings";
	}
	/**
	 * 设置表格
	 * @return
	 * @throws Exception
	 */
	public String settingtable() throws Exception{
		
		Messages m=new Messages();
		PrintWriter out = response.getWriter();
		Gson gson = new Gson();
		try {
			String value=request.getParameter("setting_value").toString();
			String key=request.getParameter("setting_key").toString();
			m=this.userService.editTableview(key,value,session);
		} catch (Exception e) {
			e.printStackTrace();
			m.setInfo(false);
			m.setText(e.getMessage());
		}
		out.print(gson.toJson(m));
		
		out.flush();
		out.close();
		return NONE;
	}
	/**
	 * 查看角色
	 * @return
	 * @throws Exception
	 */
	public String rolelist() throws Exception{
		logger.info("查看角色列表");
		try {
			this.roleService.getAllRole(currentPage,sortField,orderType,maxrecord,request);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return "role_list";
	}
	
	/**
	 * 查看角色 权限
	 * @return
	 */
	public String rolemenu(){
		return "role_menu";
	}
	/**
	 * 查看角色列表
	 * @return
	 */
	public String loadRoleMenu(){
		PrintWriter out = null;
		try {
			out = response.getWriter();
			List<Map<String, Object>> menuTreeList = this.roleService
					.loadMenuTree(roleid);
			Gson gson = new Gson();
			String jsonStr = gson.toJson(menuTreeList);
			out.print(jsonStr);
		} catch (IOException e) {
			e.printStackTrace();
		}catch (Exception e) {
			e.printStackTrace();
		}
		out.flush();
		out.close();
		return NONE;
	}
	/**
	 * 修改角色权限菜单列表
	 * @return
	 * @throws Exception
	 */
	public String editRoleMenu()throws Exception{
		PrintWriter out = response.getWriter();
		Gson gson = new Gson();
		Messages m= new Messages();
		try {
			m= this.roleService.editRoleMenu(roleid,menuid);
		} catch (Exception e) {
			e.printStackTrace();
			m.setInfo(false);
			m.setText(e.getMessage());
		}
		
		out.print(gson.toJson(m));
		
		out.flush();
		out.close();
		return NONE;
	}
	/**
	 * 修改角色属性
	 * @return
	 * @throws Exception
	 */
	public String editRole() throws Exception{
		Messages m=new Messages();
		PrintWriter out = response.getWriter();
		Gson gson = new Gson();
		try {
			m= this.roleService.editRole(this.role);
		} catch (Exception e) {
			m.setInfo(false);
			m.setText(e.getMessage());
		}
		out.print(gson.toJson(m));
		out.flush();
		out.close();
		return NONE;
	}
	/**
	 * 删除角色
	 * @return
	 */
	public String delRole() throws Exception{
		//deleltRole
		Messages m=new Messages();
		PrintWriter out = response.getWriter();
		Gson gson = new Gson();
		try {
			m= this.roleService.deleltRole(roleid);
		} catch (Exception e) {
			m.setInfo(false);
			m.setText(e.getMessage());
			e.printStackTrace();
		}
		out.print(gson.toJson(m));
		
		out.flush();
		out.close();
		return NONE;
	}
	/**
	 * 新建角色
	 * @return
	 * @throws Exception
	 */
	public String addRole() throws Exception{
		System.out.println(this.role.getRole_name());
		PrintWriter out = response.getWriter();
		Gson gson = new Gson();
		Messages m= new Messages();
		try {
			m= this.roleService.addRole(this.role);
		} catch (Exception e) {
			e.printStackTrace();
			m.setInfo(false);
			m.setText(e.getMessage());
		}
		out.print(gson.toJson(m));
		
		out.flush();
		out.close();
		return  NONE;
	}
	/**
	 * 查看用户列表
	 * @return
	 * @throws Exception
	 */
	
	public String userlist() throws Exception{
		logger.info("查看用户列表");
		try {
			this.userService.getUserAll(currentPage,sortField,orderType,maxrecord,request);
			request.setAttribute("roleList", this.roleService.getAlllist());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return "user_list";
	}
	/**
	 * 修改用户角色
	 * @return
	 * @throws Exception
	 */
	
	public String editUserRole() throws Exception{
		Messages m=new Messages();
		PrintWriter out = response.getWriter();
		Gson gson = new Gson();
		System.out.println("修改"+user.getUser_account()+"角色为：");
		System.out.println(this.role_id_array);
		try {
			m= this.userService.editUserRole(user,role_id_array);
		} catch (Exception e) {
			m.setInfo(false);
			m.setText(e.getMessage());
		}
		out.print(gson.toJson(m));
		
		out.flush();
		out.close();
		return NONE;
	}
	/**
	 * 获取用户+角色信息 并且回填
	 * @return
	 * @throws Exception
	 */
	public String loadUserinfo() throws Exception{
		Map<String,Object> mapObj=new HashMap<String,Object>();
		PrintWriter out = null;
		Gson gson =new GsonBuilder().setExclusionStrategies(new ExclusionStrategy() {
			public boolean shouldSkipField(FieldAttributes f) {
				String name=f.getName();
				if(name.equals("user_account")||name.equals("roleid")||name.equals("role_id")||name.equals("role_name"))
					return false;
				return true;
			}
			public boolean shouldSkipClass(Class<?> clazz) {
				// TODO Auto-generated method stub
				return false;
			}
		}).create();
		try {
			out=response.getWriter();
			mapObj=this.userService.getUserByID(user_id);
		} catch (Exception e) {
			mapObj.put("info", false);
			mapObj.put("text", e.getMessage());
		}
		out.print(gson.toJson(mapObj));
		out.flush();
		out.close();
		
		return NONE;
	}
	public String editserial() throws Exception{
		Messages m=new Messages();
		PrintWriter out = response.getWriter();
		Gson gson = new Gson();
		try {
			m=this.userService.setSerial(request,this.configpath);
		} catch (Exception e) {
			e.printStackTrace();
			m.setInfo(false);
			m.setText(e.getMessage());
		}
		out.print(gson.toJson(m));
		
		out.flush();
		out.close();
		return NONE;
	}
	/**
	 * 重新登录
	 * @return
	 */
	public String refsign() throws Exception{
//		PrintWriter out = response.getWriter();
//		Gson gson = new Gson();
//		Messages m=new Messages();
		try {
			session.invalidate();
//			m.setInfo(true);
//			m.setText("已成功退出，感谢您的使用");
		} catch (Exception e) {
//			m.setInfo(false);
//			m.setText(e.getMessage());
		}
//		out.print(gson.toJson(m));
//		out.flush();
//		out.close();
		return "refsign";
	}
	public String getRole_id_array() {
		return role_id_array;
	}
	public void setRole_id_array(String role_id_array) {
		this.role_id_array = role_id_array;
	}
	public String getUser_id() {
		return user_id;
	}
	public void setUser_id(String user_id) {
		this.user_id = user_id;
	}
	public UserBean getUser() {
		return user;
	}
	public void setUser(UserBean user) {
		this.user = user;
	}
	public String getMenuid() {
		return menuid;
	}
	public void setMenuid(String menuid) {
		this.menuid = menuid;
	}
	public String getRolename() {
		return rolename;
	}
	public void setRolename(String rolename) {
		this.rolename = rolename;
	}
	public RoleBean getRole() {
		return role;
	}
	public void setRole(RoleBean role) {
		this.role = role;
	}
	public String getRoleid() {
		return roleid;
	}
	public void setRoleid(String roleid) {
		this.roleid = roleid;
	}
}
